package com.lzx.applib.okhttp.download;

import android.content.ContentValues;
import android.database.Cursor;
import android.support.annotation.IntDef;
import android.text.TextUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Created by Lzx on 16/11/28 0028.
 */
public class DownloadInfo implements TableConstants {

    public static final String TAG = "DownloadInfo";

    @IntDef({WAITING, LOADING, PAUSE, FINISH, ERROR})
    @Retention(RetentionPolicy.SOURCE)
    public @interface TaskState {
    }

    public static final int WAITING = 0;
    public static final int LOADING = 1;
    public static final int PAUSE = 2;
    public static final int FINISH = 3;
    public static final int ERROR = 4;

    public int id = -1;
    public String remotePath;
    public String localPath;
    public long doneBytes;
    public long allBytes;
    @DownloadInfo.TaskState
    public int state;
    public String other;

    public DownloadInfo(String remotePath, String localPath, String other) {
        this.remotePath = remotePath;
        this.localPath = localPath;
        this.doneBytes = 0;
        this.allBytes = -1;
        this.state = WAITING;
        this.other = other;
    }

    public DownloadInfo(String remotePath, String localPath, long doneBytes, long allBytes, int state, String other) {
        this.remotePath = remotePath;
        this.localPath = localPath;
        this.doneBytes = doneBytes;
        this.allBytes = allBytes;
        this.state = state;
        this.other = other;
    }

    public DownloadInfo(Cursor cursor) {
        id = cursor.getInt(cursor.getColumnIndex(ID));
        remotePath = cursor.getString(cursor.getColumnIndex(REMOTE));
        localPath = cursor.getString(cursor.getColumnIndex(LOCAL));
        state = getState(cursor.getInt(cursor.getColumnIndex(STATE)));
        doneBytes = cursor.getInt(cursor.getColumnIndex(DONE));
        allBytes = cursor.getInt(cursor.getColumnIndex(ALL));
        other = cursor.getString(cursor.getColumnIndex(OTHER));
    }

    public ContentValues toContentValues() {
        ContentValues values = new ContentValues();
        values.put(REMOTE, remotePath);
        values.put(LOCAL, localPath);
        values.put(STATE, state);
        values.put(DONE, doneBytes);
        values.put(ALL, allBytes);
        values.put(OTHER, other);

        return values;
    }

    public
    @DownloadInfo.TaskState
    int getState(int i) {
        switch (i) {
            case 0:
                return WAITING;
            case 1:
                return LOADING;
            case 2:
                return PAUSE;
            case 3:
                return FINISH;
            case 4:
                return ERROR;
        }
        return LOADING;
    }

    @Override
    public String toString() {
        return "DownloadInfo{" +
                "id=" + id +
                ", remotePath='" + remotePath + '\'' +
                ", localPath='" + localPath + '\'' +
                ", doneBytes=" + doneBytes +
                ", allBytes=" + allBytes +
                ", state=" + state +
                '}';
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof  DownloadInfo) {
            return TextUtils.equals(((DownloadInfo) obj).remotePath, remotePath);
        } else {
            return false;
        }
    }
}
